// components/navigation/navigation.js
import Request from '../../utils/request.js'
import { checkEmoji } from '../../utils/util.js'
let app = getApp()
let store = app.store
Component({
  properties: {
    //导航栏背景色
    backgroundColor: {
      type: String,
      default: "#F9F9F9"
    },
    //小程序页面的表头
    title: {
      type: String,
      default: ''
    },
    //表头的颜色
    titleColor: {
      type: String,
      default: "#2E323A"
    },
    //是否展示返回和主页按钮
    showIcon: {
      type: Boolean,
      default: true
    },
    //是否展示返回按钮
    showBackIcon: {
      type: Boolean,
      default: false
    },
    //左边文字
    leftText: {
      type: String,
      default: ""
    },
    leftTextColor: {
      type: String,
      default: "rgba(255, 255, 255, 1);"
    },
    //是否显示 搜索框
    showSearch:{
      type: Boolean,
      default: false
    },
    showCancel:{
      type:Boolean,
      default:false
    },
    inputVal:{
      type:String,
      default:''
    },
    autoFocus: {
      type: Boolean,
      value: true
    },
    placeholder: {
      type: String,
      value: "搜索医生，科室"
    }
  },

  data: {
    statusBarHeight: 0,
    titleBarHeight: 0,
  },

  ready: function() {
    // 因为很多地方都需要用到，所有保存到全局对象中
    if (app.globalData && app.globalData.statusBarHeight && app.globalData.titleBarHeight) {
      this.setData({
        statusBarHeight: app.globalData.statusBarHeight,
        titleBarHeight: app.globalData.titleBarHeight
      });
    } else {
      let that = this
      wx.getSystemInfo({
        success: function(res) {
          // if (!app.globalData) {
          //   app.globalData = {}
          // }
          if (res.model.indexOf('iPhone') !== -1) {
            app.globalData.titleBarHeight = 44
          } else {
            app.globalData.titleBarHeight = 48
          }
          app.globalData.statusBarHeight = res.statusBarHeight
          that.setData({
            statusBarHeight: app.globalData.statusBarHeight,
            titleBarHeight: app.globalData.titleBarHeight
          });
        },
        failure() {
          that.setData({
            statusBarHeight: 0,
            titleBarHeight: 0
          });
        }
      })
    }
  },

  methods: {
    headerBack() {
      wx.navigateBack({
        delta: 1,
      })
    },
    headerHome() {
      wx.switchTab({
        url: '/pages/index/index'
      })
    },
    goSearch: function() {
      wx.navigateTo({
        url: "/pages/home/search/search"
      });
    },
    searchHandle(event){
      console.log(event.detail);
      if (checkEmoji(event.detail.value)) {
        wx.showToast({
          icon: 'none',
          title: '搜索内容不能包含表情',
          duration: 3000
        })
        return
      }
      this.triggerEvent('SearchResultData', event.detail)
    },
    tapHandler() {
      this.triggerEvent('tapheader')
    }
  }
})